import pygame
from pygame.sprite import Sprite

class Ship(Sprite):
    def __init__(self, ai_game):
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.screen_rect = ai_game.screen.get_rect()
        # 加载图片
        self.image = pygame.image.load('.\images\ship.bmp')
        # 重置飞船图片的大小
        self.image = pygame.transform.scale(self.image, (self.settings.ship_transform_width, self.settings.ship_transform_height))
        # Surface.get_rect()返回起点为(0,0)的长度和宽度和image一样的矩形
        self.rect = self.image.get_rect()
        # 重置飞船在display的中间底部位置
        self.rect.midbottom = self.screen_rect.midbottom
        self.x = float(self.rect.x)
        self.moving_right = False
        self.moving_left = False
        
    # 向左右移动飞船
    def update(self):
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.x += self.settings.ship_speed
        if self.moving_left and self.rect.left > 0:
            self.x -= self.settings.ship_speed
        # 更新飞船位置
        self.rect.x = self.x

    # 将飞船绘制到screen的中间底部位置
    def blitme(self):
        self.screen.blit(self.image, self.rect)
    
    def center_ship(self):
        self.rect.midbottom = self.screen_rect.midbottom
        # self.x = float(self.rect.x)